﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace CSharp1
{
    public class Messager
    {
        private const string API = "http://api.douqq.com/?key=a1g9S0tuMmVBVjg9QjdUUXFncGo4QWttc0E4QUFBPT0&msg=";
        private WebRequest request;
        public Messager()
        {

        }

        private WebRequest GetRequest(string url)
        {
            return WebRequest.Create(url);
        }

        /// <summary>
        /// 获取问题答案
        /// </summary>
        /// <param name="question">问题</param>
        /// <returns>答案</returns>
        public string GetAnswer(string question)
        {
            try
            {
                WebRequest request = GetRequest(API + question);
                request.Method = "GET";
                using (WebResponse answer = request.GetResponse())
                {
                    using (Stream s = answer.GetResponseStream())
                    {
                        StreamReader reader = new StreamReader(s, Encoding.UTF8);
                        return reader.ReadToEnd();
                    }
                }
            }
            catch (Exception)
            {
                return "暂无无法回答问题";
            }
        }

    }
}
